/*********************************
name:		kayla freeman 
date:		6/14/21
purpose:	prep of sample with tc data, regressions, tables, figures for everything before contract analysis
**********************************/

clear

use 04_prepped_data
destring s_gvkey c_gvkey, replace

sort s_gv year
bysort s_gv year: gen numcheck = _N

/*keep only yeses*/
keep if na=="yes"

/*gen a dummy for all custs repped for robustnesscheck*/
bysort s_gv year: gen allofem = _N==numcheck

/*mergein rauch/gbe differentiation data*/
gen sic2digit = substr(s_sic,1,2)
destring sic2digit, replace
merge m:1 sic2digit using 05_differentiation_data
drop if _merge==2
drop _merge
drop sic2digit
foreach var of varlist desc serv diff_goods std_goods serv_inputs diff_inputs std_inputs{
	ren `var' s_`var'
}
gen sic2digit = substr(c_sic,1,2)
destring sic2digit, replace
merge m:1 sic2digit using 05_differentiation_data
drop if _merge==2
drop _merge
drop sic2digit
foreach var of varlist desc serv diff_goods std_goods serv_inputs diff_inputs std_inputs{
	ren `var' c_`var'
}


/*prop sale*/
gen prop_sale = csale/s_revt
gen ln_prop = ln(prop_sale*100)
label var prop_sale "share of supplier's sales"
label var ln_prop "Sales dependence"


*fill in ar_amount & ar_perc
gen ar_type = .
replace ar_type = 1 if !missing(ar_amount)
replace ar_type = 2 if !missing(ar_perc)
label var ar_type "1 if 10k reported amt, 2 if reported %, else missing"
replace ar_amount = ar_amount/1000000
replace ar_amount = ar_perc*s_rect if missing(ar_amount)
replace ar_perc = ar_amount/s_rect if missing(ar_perc)

*relationship length
gen rel_length = ln(1+year-scyr1)

*lnnum partners
gen ln_numcust = ln(numcust)
gen ln_numsupp = ln(numsupp)

*define tc vars
gen s_arA = s_rect/s_at
gen s_arS = s_rect/s_revt

/*chg csale*/
sort s_gvkey c_gvkey year
by s_gvkey c_gvkey: gen chgcsale = csale/csale[_n-1] if year ==year[_n-1]+1

*gen more specific ar vars
*vars based on %receivables - individual, agg(indiv), and other
sort s_gvkey year
bysort s_gvkey year: egen agg_ar_perc = total(ar_perc) /*agg total percent of rect going to repped customers*/
format agg_ar_perc %10.0g
gen o_ar_perc = 1 - agg_ar_perc /*agg total percent of rect going to non-repped customers*/

order ar_perc agg_ar_perc o_ar_perc			
sort s_gvkey year 
format o_ar_perc agg_ar_perc %10.0g
label var ar_perc "proportion of rec to indiv customer"
label var agg_ar_perc "proportion of rec to agg reported eceivables"
label var o_ar_perc "proportion of rec not reported in sample"
count if o_ar_perc == 1 - agg_ar_perc 

*vars based on %ar/sale ratio - individual, other, and all-other
gen sp_arS = ar_amount/csale

sort s_gvkey year
bysort s_gvkey year: egen sum_csale = total(csale)
bysort s_gvkey year: egen sum_aram = total(ar_amount)

label  var sp_arS "TC/sales"
label var sum_csale "total supplier sales to reported customers in sample"
label var sum_aram "total supplier receivables from reported customers in sample"

gen aggmaj_arS = sum_aram/sum_csale
gen aggmaj_prop = sum_csale/s_revt
	replace aggmaj_prop = ln(aggmaj_prop*100)

gen aggmin_arS = (s_rect-sum_aram)/(s_revt-sum_csale)

label var aggmaj_arS "Trade Credit to Agg. Majors"
label var aggmaj_prop "Dependence on Agg. Majors"

ren s_lnage s_age
ren c_lnage c_age

egen cs_id = group(s_id c_id)

*merge in 5 Dealscan files: 
capture drop _merge
merge m:1 s_gvkey year using 08_supplierDS
	drop if _merge==2 /*s loans for syears not in my data*/
	replace hasloan = 0 if _merge==1 /*supps in my data that do not link to ds that year*/
	capture drop everds 
	capture drop firstfacyr
	replace kcon=0 if hasloan==1 & missing(kcon) & !missing(valid)
	drop _merge
merge m:1 s_gvkey using 06_everDS
	replace everds = 0 if _merge!=3
	drop if _merge==2
	drop _merge
merge 1:1 c_gvkey s_gvkey year using 09_cloan
	drop if _merge==2 /*c loans for cyear not in my data*/
	replace c_hasloan=0 if _merge==1
	drop _merge
merge 1:1 c_gvkey s_gvkey year using 08_commons
	drop if _merge==2
	replace commlen = 0 if c_hasloan==1 & hasloan==1 &_merge==1 /*leave it missing if these aren't true*/
	replace num_commls = 0 if c_hasloan==1 & hasloan==1 &_merge==1 
	drop _merge

/*merge in port defaults*/
ren s_gvkey gvkey
merge m:1 gvkey year using 11_portdefaults 
drop if _merge==2
gen defuse=_merge==3
drop _merge
ren gvkey s_gvkey

capture drop use
gen use=1
global svars   s_lnat s_blev s_profit s_hhi    s_age
global cvars   c_lnat  c_blev c_profit c_hhi    c_age
global relvars ln_prop
global clustvar s_id  c_id

replace hasloan = . if year>2016
replace hasloan = . if firstfacyr>2016
replace everds = . if firstfacyr>2016
replace hasloan = . if everds==.
replace hasloan = . if firstfacyr>year & everds==1 &hasloan==0


/*c prob default*/
gen str6 gvkey_str = string(c_gvkey,"%06.0f")
gen fyear = year
merge m:1 gvkey_str fyear using 00_dd_annual.dta,keepusing(dd pi)
drop if _merge==2
drop _merge

capture drop gvkey_str fyear
capture drop distress

/* ratings*/

gen c_rate = .
	replace c_rate = 22 if c_rating=="AAA"
	replace c_rate = 21 if c_rating=="AA+"
	replace c_rate = 20 if c_rating=="AA"
	replace c_rate = 19 if c_rating=="AA-"
	replace c_rate = 18 if c_rating=="A+"
	replace c_rate = 17 if c_rating=="A"
	replace c_rate = 16 if c_rating=="A-"
	replace c_rate = 15 if c_rating=="BBB+"
	replace c_rate = 14 if c_rating=="BBB"
	replace c_rate = 13 if c_rating=="BBB-"
	replace c_rate = 12 if c_rating=="BB+"
	replace c_rate = 11 if c_rating=="BB"
	replace c_rate = 10 if c_rating=="BB-"
	replace c_rate = 9 if c_rating=="B+"
	replace c_rate = 8 if c_rating=="B"
	replace c_rate = 7 if c_rating=="B-"
	replace c_rate = 6 if c_rating=="CCC+"
	replace c_rate = 5 if c_rating=="CCC"
	replace c_rate = 4 if c_rating=="CCC-"
	replace c_rate = 3 if c_rating=="CC"
	replace c_rate = 2 if c_rating=="C"
	replace c_rate = 1 if c_rating=="D"
	
gen cinvgr = 0 if !missing(c_rate)
	replace cinvgr = 1 if c_rate>12 &!missing(c_rate)

gen s_rate = .
	replace s_rate = 22 if s_rating=="AAA"
	replace s_rate = 21 if s_rating=="AA+"
	replace s_rate = 20 if s_rating=="AA"
	replace s_rate = 19 if s_rating=="AA-"
	replace s_rate = 18 if s_rating=="A+"
	replace s_rate = 17 if s_rating=="A"
	replace s_rate = 16 if s_rating=="A-"
	replace s_rate = 15 if s_rating=="BBB+"
	replace s_rate = 14 if s_rating=="BBB"
	replace s_rate = 13 if s_rating=="BBB-"
	replace s_rate = 12 if s_rating=="BB+"
	replace s_rate = 11 if s_rating=="BB"
	replace s_rate = 10 if s_rating=="BB-"
	replace s_rate = 9 if s_rating=="B+"
	replace s_rate = 8 if s_rating=="B"
	replace s_rate = 7 if s_rating=="B-"
	replace s_rate = 6 if s_rating=="CCC+"
	replace s_rate = 5 if s_rating=="CCC"
	replace s_rate = 4 if s_rating=="CCC-"
	replace s_rate = 3 if s_rating=="CC"
	replace s_rate = 2 if s_rating=="C"
	replace s_rate = 1 if s_rating=="D"
	
gen sinvgr = 0 if !missing(s_rate)
	replace sinvgr = 1 if s_rate>12 &!missing(s_rate)

keep if  year<2017 /*end of sample period*/

keep if !missing(s_hhi, s_age, s_lnat, s_blev, s_profit, c_hhi, s_age, c_lnat, c_blev, c_profit, sp_arS, prop_sale)

save 12_PreKData,replace

winsor2 ar_perc agg_ar_perc o_ar_perc aggmaj_arS aggmin_arS aggmaj_prop    s_hhi s_mkt_share s_ap_at s_ar_at s_ap_cogs s_ar_revt s_lnat s_q s_tang  s_profit c_hhi c_mkt_share c_ap_at c_ar_at c_ap_cogs s_age c_age ///
	c_ar_revt c_lnat c_q c_tang  c_profit prop_sale ln_prop rel_length ln_numcust ln_numsupp s_arA s_arS sp_arS   s_rdat c_rdat s_curdat c_curdat,replace

replace s_blev = 1 if s_blev>1
replace c_blev = 1 if c_blev>1
replace s_blev = 0 if s_blev<0
replace c_blev = 0 if c_blev<0

label var s_age "S Age"
label var c_age "C Age"
label var s_lnat "S Size"
label var c_lnat "C Size"
label var s_profit "S Profitability"
label var c_profit "C Profitability"
label var s_hhi "S HHI"
label var c_hhi "C HHI"
label var rel_length "Relationship Length"
label var sp_arS "Trade Credit"
label var ln_prop "Sales Dependence"
label var s_blev "S Leverage"
label var c_blev "C Leverage"


global svars   s_lnat s_blev s_profit s_hhi    s_age
global cvars   c_lnat  c_blev c_profit c_hhi    c_age
global relvars ln_prop  
global clustvar s_id  c_id


/*
TABLE 1: SUMMARY STATISTICS*/
 estpost summarize  sp_arS $relvars $svars   $cvars  ,  det

 /*
 TABLE 2: BASELINE
 */
  capture drop use
 gen use=1 

qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
 eststo firms
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
qui reghdfe sp_arS    $relvars   $cvars , absorb(year#s_id c_id) vce(cluster $clustvar ), if use ==1
eststo  sy_x
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local syfe "Yes", replace
 reghdfe sp_arS  $relvars   $svars , absorb(year#c_id s_id) vce(cluster $clustvar ) , if use ==1
eststo  cy_x
		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cyfe "Yes", replace
qui reghdfe sp_arS $relvars   , absorb(year#s_id year#c_id,savefe) vce(cluster $clustvar ), if use ==1
eststo  scy_x
		 quietly estadd local cyfe "Yes", replace
		 quietly estadd local syfe "Yes", replace

estout firms cs sy_x cy_x scy_x, order( ln_prop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab firms cs sy_x cy_x scy_x ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(sfe cfe csfe yfe syfe cyfe r2_a N, fmt(%20s %20s %20s %20s %20s %20s %9.3fc %9.0fc)label("Supplier FE" "Customer FE" "Pair FE" "Year FE" "Supplier x Year FE" "Customer x Year FE" "R2" "Observations"))  label 
	title("Baseline: Effect of Sales Dependence on Trade Credit")  collabels(none)   nomtitles
	order( ln_prop  ) drop( _cons) 
;
#delimit cr

/*
TABLE 3: TRADE CREDIT ACROSS PORTFOLIO
*/
sort s_gvkey year csale
capture drop first
by s_gv year: gen first = _n==_N
replace use = first==1

qui reghdfe s_ar_revt  aggmaj_prop   $svars  , absorb(year s_id) vce(cluster s_id ),  if use==1
	eststo allar
	qui estadd local schar "Yes",replace
	qui estadd local firmfe "Yes",replace
	qui estadd local yearfe "Yes",replace
	
qui reghdfe aggmaj_arS  aggmaj_prop   $svars  , absorb(year s_id) vce(cluster s_id ),  if use==1
	eststo majar
	qui estadd local schar "Yes",replace
	qui estadd local firmfe "Yes",replace
	qui estadd local yearfe "Yes",replace

qui reghdfe aggmin_arS  aggmaj_prop   $svars  , absorb(year s_id) vce(cluster s_id ),  if use==1
	eststo minar
	qui estadd local schar "Yes",replace
	qui estadd local firmfe "Yes",replace
	qui estadd local yearfe "Yes",replace
estout allar majar minar, order( aggmaj_prop ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 
# delimit ;
esttab allar majar minar  ,  booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) label stats(schar firmfe yearfe r2_a N ,fmt(%20s %20s %20s %9.3fc %9.0fc) label("Firm Characteristics" "Firm FE" "Year FE"  "R2" "Observations")) 
	collabels(none)   nomtitles
	keep(aggmaj_prop) 
;
#delimit cr

/*************************
TABLE IA-2: ROBUSTNESS CHECKS WITH ALT DEP AND TC MEASURES
*************************/
/*changes*/
capture drop chgprop chgtc
sort s_gv c_gv year
gen chgprop = . 
gen chgtc = .
capture drop chgcsale
gen chgcsale = .
by  s_gv c_gv: replace chgprop = (csale/s_revt) - (csale[_n-1]/s_revt[_n-1]) if year ==year[_n-1]+1
by  s_gv c_gv: replace chgtc = (ar_amount/csale) - (ar_amount[_n-1]/csale[_n-1]) if year == year[_n-1]+1
by  s_gv c_gv: replace chgcsale = (csale)/(csale[_n-1]) if year ==year[_n-1]+1

winsor2 chgprop chgtc chgcsale,replace
replace use =1

 reghdfe chgtc  chgprop  $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1 
 eststo firms
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe chgtc  chgprop  $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
qui reghdfe chgtc   chgprop  $cvars , absorb(year#s_id c_id) vce(cluster $clustvar ), if use ==1
eststo sy_x
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local syfe "Yes", replace
 reghdfe chgtc chgprop  $svars , absorb(year#c_id s_id) vce(cluster $clustvar ) , if use ==1
eststo cy_x
		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cyfe "Yes", replace
qui reghdfe chgtc chgprop  , absorb(year#s_id year#c_id) vce(cluster $clustvar ), if use ==1

estout firms cs sy_x cy_x , order( ln_prop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab firms cs sy_x cy_x  ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(sfe cfe csfe yfe syfe cyfe r2_a N, fmt(%20s %20s %20s %20s %20s %20s %9.3fc %9.0fc)label("Supplier FE" "Customer FE" "Pair FE" "Year FE" "Supplier x Year FE" "Customer x Year FE" "R2" "Observations"))  label 
	title("Robustness: Effect of Sales Dependence Changes on Trade Credit Changes")  collabels(none)   nomtitles
	order( ln_prop  ) drop( _cons) 
;
#delimit cr

/*changes, %change in ratios*/
capture drop chgprop chgtc
sort s_gv c_gv year
gen chgprop = . 
gen chgtc = .
capture drop chgcsale
gen chgcsale = .
by  s_gv c_gv: replace chgprop = (csale/s_revt)/(csale[_n-1]/s_revt[_n-1])-1 if year ==year[_n-1]+1
by  s_gv c_gv: replace chgtc = (ar_amount/csale)/(ar_amount[_n-1]/csale[_n-1])-1 if year == year[_n-1]+1
by  s_gv c_gv: replace chgcsale = (csale)/(csale[_n-1]) if year ==year[_n-1]+1

winsor2 chgprop chgtc chgcsale,replace

qui reghdfe chgtc  chgprop  $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
 eststo firms
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe chgtc  chgprop  $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
qui reghdfe chgtc   chgprop  $cvars , absorb(year#s_id c_id) vce(cluster $clustvar ), if use ==1
eststo sy_x
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local syfe "Yes", replace
 reghdfe chgtc chgprop  $svars , absorb(year#c_id s_id) vce(cluster $clustvar ) , if use ==1
eststo cy_x
		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cyfe "Yes", replace

estout firms cs sy_x cy_x , order( ln_prop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab firms cs sy_x cy_x  ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(sfe cfe csfe yfe syfe cyfe r2_a N, fmt(%20s %20s %20s %20s %20s %20s %9.3fc %9.0fc)label("Supplier FE" "Customer FE" "Pair FE" "Year FE" "Supplier x Year FE" "Customer x Year FE" "R2" "Observations"))  label 
	title("Robustness: Effect of Sales Dependence Changes on Trade Credit Changes")  collabels(none)   nomtitles
	order( ln_prop   ) drop( _cons) 
;
#delimit cr

/*ctrl for salesgr*/
winsor2 salesgr,replace
replace use =1
qui reghdfe sp_arS  $relvars salesgr  $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
eststo firms
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe sp_arS   $relvars salesgr  $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
qui reghdfe sp_arS    $relvars  salesgr $cvars , absorb(year#s_id c_id) vce(cluster $clustvar ), if use ==1
eststo sy_x
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local syfe "Yes", replace
qui reghdfe sp_arS  $relvars salesgr  $svars , absorb(year#c_id s_id) vce(cluster $clustvar ) , if use ==1
eststo cy_x
		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cyfe "Yes", replace

estout firms cs sy_x cy_x  , order( ln_prop   ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab firms cs sy_x cy_x   ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(schars cchars relchars sfe cfe yfe  cyfe  r2_a N ,fmt( %20s %20s %20s %20s  %20s %20s %20s  %9.3fc %9.0fc) label("Supplier Characteristics" "Customer Characteristics" "Relationship Characteristics" "Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Robustness checks")  collabels(none)   nomtitles
	order( ln_prop salesgr   ) keep( ln_prop salesgr) 
;
#delimit cr


/******************
TABLE IA-5, ALL MAJOR CUSTS REPPED
*******************/
replace use = allofem==1
qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
eststo firms
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
qui reghdfe sp_arS    $relvars   $cvars , absorb(year#s_id c_id) vce(cluster $clustvar ), if use ==1
eststo sy_x
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local syfe "Yes", replace
qui reghdfe sp_arS  $relvars   $svars , absorb(year#c_id s_id) vce(cluster $clustvar ) , if use ==1
eststo cy_x
		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cyfe "Yes", replace
qui reghdfe sp_arS $relvars   , absorb(year#s_id year#c_id) vce(cluster $clustvar ), if use ==1
eststo scy_x
		 quietly estadd local cyfe "Yes", replace
		 quietly estadd local syfe "Yes", replace

estout firms cs sy_x cy_x  , order( ln_prop   ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab firms cs sy_x cy_x   ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(schars cchars relchars sfe cfe yfe syfe cyfe  r2_a N ,fmt( %20s %20s %20s %20s %20s %20s %20s %20s  %9.3fc %9.0fc) label("Supplier Characteristics" "Customer Characteristics" "Relationship Characteristics" "Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Robustness checks")  collabels(none)   nomtitles
	order( ln_prop   ) keep( ln_prop) 
;
#delimit cr

/****************
TABLE IA-6,PANEL A: GROSS MARGINS
***************/

capture drop sgm
gen sgm = s_revt/s_cogs
winsor2 sgm,replace

capture drop avgspars
bys s_id year: egen avgspars = mean(sp_arS)
capture drop first
sort s_id year -csale
by s_id year : gen first = _n==1
replace use =1
qui reghdfe sgm sp_arS $relvars $cvars  $svars  , absorb(year c_id s_id) vce(cluster s_id ), if use==1
	eststo pairs
	qui estadd local sfe "Yes",replace
	qui estadd local cfe "Yes",replace
	qui estadd local yfe "Yes",replace
	qui estadd local schars "Yes",replace
	qui estadd local cchars "Yes",replace
	qui estadd local relchars "Yes",replace
	qui estadd local panel "Pair-year",replace
qui reghdfe sgm  avgspars   $svars   , absorb(year   s_id) vce(cluster s_id ),  if use==1  &first==1
 eststo firmavg
	qui estadd local sfe "Yes",replace
	qui estadd local yfe "Yes",replace
	qui estadd local schars "Yes",replace
	qui estadd local panel "Firm-year",replace

qui reghdfe sgm  s_ar_revt   $svars   , absorb(year   s_id) vce(cluster s_id ),  if use==1  &first==1
 eststo firmall
	qui estadd local sfe "Yes",replace
	qui estadd local yfe "Yes",replace
	qui estadd local schars "Yes",replace
	qui estadd local panel "Firm-year",replace

	 estout pairs firmavg firmall, order( sp_arS avgspars s_ar_revt ln_prop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab pairs firmavg firmall using ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) label stats(schars cchars relchars sfe cfe yfe syfe cyfe  r2_a N ,fmt( %20s %20s %20s %20s %20s %20s %20s %20s  %9.3fc %9.0fc) label("Supplier Characteristics" "Customer Characteristics" "Relationship Characteristics" "Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  
	title("Gross Margins")  collabels(none)   mtitles("Pair TC" "Avg. Pair TC" "Agg. TC")
	order( sp_arS avgspars s_ar_revt ) keep( sp_arS avgspars s_ar_revt) 
;
#delimit cr

/********************
TABLE IA-6, Panels B-D and IA-7
********************/

	/*for IA-6,B, std goods*/
replace use = s_std_good==1

	/*for IA-6,C, low market share*/
sum s_mkt_sh,det
	replace use = s_mkt_sh<r(p50)
	/*for IA-6,D, competitive industries*/
sum s_hhi,det
	replace use = s_hhi<r(p50)
	/*for IA-7, rel_length*/
capture drop use
sum rel_length,det
gen use = rel_length>r(p50)

qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
eststo firms
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
qui reghdfe sp_arS    $relvars   $cvars , absorb(year#s_id c_id) vce(cluster $clustvar ), if use ==1
eststo sy_x
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local syfe "Yes", replace
qui reghdfe sp_arS  $relvars   $svars , absorb(year#c_id s_id) vce(cluster $clustvar ) , if use ==1
eststo cy_x
		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cyfe "Yes", replace
qui reghdfe sp_arS $relvars   , absorb(year#s_id year#c_id) vce(cluster $clustvar ), if use ==1
eststo scy_x
		 quietly estadd local cyfe "Yes", replace
		 quietly estadd local syfe "Yes", replace

estout firms cs sy_x cy_x  , order( ln_prop   ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab firms cs sy_x cy_x   ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(schars cchars relchars sfe cfe yfe syfe cyfe  r2_a N ,fmt( %20s %20s %20s %20s %20s %20s %20s %20s  %9.3fc %9.0fc) label("Supplier Characteristics" "Customer Characteristics" "Relationship Characteristics" "Supplier FE" "Customer FE" "Year FE" "R2" "Observations"))  label 
	title("Robustness checks")  collabels(none)   nomtitles
	order( ln_prop   ) keep( ln_prop) 
;
#delimit cr
/*
TABLE IA-8: MONITORING
*/
replace use = hasloan==1   
qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
 eststo firms1
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs1
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace


replace use = hasloan== 0 & everds==1 
qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
eststo firms2
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs2
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace


capture drop use 
gen use = everds==1
capture drop bbb relx* sx_* cx_*
gen bbb = hasloan
	replace bbb= . if everds!=1
foreach var of varlist $relvars   {
	gen relx_`var' = bbb*`var'
}

foreach var of varlist $svars {
	gen sx_`var' = bbb*`var'
}
foreach var of varlist $cvars {
	gen cx_`var' = bbb*`var'
}
capture drop bbb_s_id bbb_c_id
egen bbb_s_id = group(bbb s_id)
egen bbb_c_id = group(bbb c_id)
qui reghdfe sp_arS  $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb c_id#bbb s_id#bbb) vce(cluster bbb_s_id bbb_c_id),  if !missing(bbb)
eststo firmsdiff
qui reghdfe sp_arS   $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb cs_id#bbb) vce(cluster bbb_s_id bbb_c_id), if !missing(bbb)
eststo csdiff


capture drop loanprop
gen loanprop = hasloan*ln_prop
	replace loanprop = . if everds!=1
qui reghdfe sp_arS  loanprop hasloan  $relvars   $svars , absorb(year#c_id s_id) vce(cluster $clustvar ), if use ==1
eststo cyx
		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cyfe "Yes", replace


estout firms1 firms2 firmsdiff cs1 cs2 csdiff cyx , order( ln_prop relx_ln_prop hasloan loanprop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 
		 
		 
# delimit ;
esttab firms1 firms2 firmsdiff cs1 cs2 csdiff    cyx ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(sfe cfe yfe csfe  cyfe syfe r2_a N ,  fmt( %20s %20s %20s %20s %20s %20s %9.3fc %9.0fc) label("Supplier FE" "Customer FE" "Year FE" "Pair FE"  "Customer x Year FE" "Supplier x Year FE" "R2" "Observations"))  label collabels(none)   title("Cross-Sectional Splits on Supplier Bank Monitoring")  mtitles("Monitor" "No Monitor" "Difference" "Monitor" "No Monitor" "Difference" "Monitor" "No Monitor" "Difference" "Interaction")  
	order( ln_prop hasloan loanprop relx_ln_prop ) keep(ln_prop hasloan loanprop relx_ln_prop) 
;
#delimit cr

/*
TABLE IA-8, PANEL B: BANK DEFAULT EXPOSURE
*/
capture drop use
gen use = 0 if defuse==1

	
sum percdef,det, if  nolead==0  & def12mo!=1 &def24mo!=1
replace use = 1 if percdef>=r(p75) & !missing(percdef)
	replace use = . if def12mo==1 |def24mo==1 |nolead==1 
qui reghdfe sp_arS   $relvars $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
eststo firms1
 qui estadd local cfe "Yes"
 qui estadd local sfe "Yes"
 qui estadd local yfe "Yes"
 qui estadd local ctrls "Yes"
 qui reghdfe sp_arS   $relvars $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==0
eststo  firms2
 qui estadd local cfe "Yes"
 qui estadd local sfe "Yes"
 qui estadd local yfe "Yes"
 qui estadd local ctrls "Yes"
qui reghdfe sp_arS    $relvars $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo  cs1
 qui estadd local pairfe "Yes"
 qui estadd local yfe "Yes"
 qui estadd local ctrls "Yes"
qui reghdfe sp_arS    $relvars $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==0
eststo  cs2
 qui estadd local pairfe "Yes"
 qui estadd local yfe "Yes"
 qui estadd local ctrls "Yes"

capture drop bbb relx* sx_* cx_*
gen bbb = use

foreach var of varlist $relvars   {
	gen relx_`var' = bbb*`var'
}

foreach var of varlist $svars {
	gen sx_`var' = bbb*`var'
}
foreach var of varlist $cvars {
	gen cx_`var' = bbb*`var'
}
capture drop bbb_s_id bbb_c_id
egen bbb_s_id = group(bbb s_id)
egen bbb_c_id = group(bbb c_id)
qui reghdfe sp_arS  $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb c_id#bbb s_id#bbb) vce(cluster bbb_s_id bbb_c_id),  if !missing(bbb)
eststo  firmsdiff
qui reghdfe sp_arS   $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb cs_id#bbb) vce(cluster bbb_s_id bbb_c_id), if !missing(bbb)
eststo  csdiff
capture drop propdef
gen propdef = use*ln_prop
qui reghdfe sp_arS  $relvars use $svars propdef, absorb(year#c_id   s_id ) vce(cluster $clustvar ),  if !missing(bbb)
eststo  cyintx
 qui estadd local sfe "Yes"
 qui estadd local cyfe "Yes"
 qui estadd local ctrls "Yes"

estout firms1 firms2 firmsdiff cs1 cs2 csdiff cyintx, order( ln_prop relx_ln_prop )label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab firms1 firms2 firmsdiff cs1 cs2 csdiff cyintx ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(sfe cfe yfe pair  cyfe   r2_a N ,  fmt( %20s %20s %20s %20s %20s %9.3fc %9.0fc) label("Supplier FE" "Customer FE" "Year FE" "Pair FE"  "Customer x Year FE"   "R2" "Observations"))  label collabels(none) title("Cross-Sectional Variation in Bank Default Exposure")     mtitles("High Defaults" "Low Defaults" "Difference" "High Defaults" "Low Defaults" "Difference"  "Interaction") 
	order( ln_prop  relx_ln_prop use propdef) keep(ln_prop  relx_ln_prop use propdef ) 
;
#delimit cr

/*
TABLE IA-9, PANEL A: CUSTOMER DISTRESS
*/

sum pi,det, if hasloan==1
gen distress = pi>=r(p75) /*equivalent to <25pctl distance to default*/
	replace distress = . if missing(pi)
replace use = distress==1  &hasloan==1
qui  reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1 
eststo firms1
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 qui estadd local schars "Yes",replace
		 qui estadd local cchars "Yes",replace
		 qui estadd local relchars "Yes",replace
		 
qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs1
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
		 qui estadd local schars "Yes",replace
		 qui estadd local cchars "Yes",replace
		 qui estadd local relchars "Yes",replace

replace use = distress==0  &hasloan==1
qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
 eststo firms2
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
		 qui estadd local schars "Yes",replace
		 qui estadd local cchars "Yes",replace
		 qui estadd local relchars "Yes",replace

qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs2
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace
		 qui estadd local schars "Yes",replace
		 qui estadd local cchars "Yes",replace
		 qui estadd local relchars "Yes",replace


capture drop bbb relx* sx_* cx_*
gen bbb = distress if hasloan==1

foreach var of varlist $relvars   {
	gen relx_`var' = bbb*`var'
}

foreach var of varlist $svars {
	gen sx_`var' = bbb*`var'
}
foreach var of varlist $cvars {
	gen cx_`var' = bbb*`var'
}
capture drop bbb_s_id bbb_c_id
egen bbb_s_id = group(bbb s_id)
egen bbb_c_id = group(bbb c_id)
qui reghdfe sp_arS  $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb c_id#bbb s_id#bbb) vce(cluster bbb_s_id bbb_c_id),  if !missing(bbb)
 est store firmsdiff
qui reghdfe sp_arS   $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb cs_id#bbb) vce(cluster bbb_s_id bbb_c_id), if !missing(bbb)
est store csdiff


estout firms1 firms2 firmsdiff cs1 cs2 csdiff  , order( ln_prop relx_ln_prop distress distress_prop  ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 	 

# delimit ;
esttab firms1 firms2 firmsdiff cs1 cs2 csdiff   ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(sfe cfe yfe csfe  cyfe syfe r2_a N ,  fmt( %20s %20s %20s %20s %20s %20s %9.3fc %9.0fc) label("Supplier FE" "Customer FE" "Year FE" "Pair FE"  "Customer x Year FE" "Supplier x Year FE" "R2" "Observations"))  label
	title("Cross-Sectional Splits on Customer Distress")  mtitles("Distressed" "Non-Distressed" "Difference" "Distressed" "Non-Distressed" "Difference" "Distressed" "Non-Distressed" "Difference" "Interaction")   
	order( ln_prop distress distress_prop relx_ln_prop ) keep( ln_prop distress distress_prop relx_ln_prop ) 
;
#delimit cr

/*
TABLE IA-9 B: PAYABLE DAYS
*/
capture drop paycogs hipayday*
gen paycogs =(c_ap - ar_amount)/(c_cogs-csale)
sum paycogs,det, if hasloan==1
gen hipayday = paycogs>=r(p75)
	replace hipayday =. if missing(paycogs)
	
replace use = hasloan==1   & hipayday==1
qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
 eststo firms1
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace

qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs1
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace

replace use = hasloan==1   & hipayday==0
qui reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1
eststo firms2
 		 quietly estadd local sfe "Yes",replace
		 quietly estadd local cfe "Yes",replace
		 quietly estadd local yfe "Yes", replace
qui reghdfe sp_arS   $relvars   $svars $cvars , absorb(year cs_id) vce(cluster $clustvar ), if use ==1
eststo cs2
		 quietly estadd local yfe "Yes",replace
		 quietly estadd local csfe "Yes", replace

capture drop use 
gen use = hasloan==1
capture drop bbb relx* sx_* cx_*
gen bbb = hipayday
	replace bbb= . if hasloan!=1
foreach var of varlist $relvars   {
	gen relx_`var' = bbb*`var'
}

foreach var of varlist $svars {
	gen sx_`var' = bbb*`var'
}
foreach var of varlist $cvars {
	gen cx_`var' = bbb*`var'
}
capture drop bbb_s_id bbb_c_id
egen bbb_s_id = group(bbb s_id)
egen bbb_c_id = group(bbb c_id)
qui reghdfe sp_arS  $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb c_id#bbb s_id#bbb) vce(cluster bbb_s_id bbb_c_id),  if !missing(bbb)
 est store firmsdiff
qui reghdfe sp_arS   $relvars   $svars $cvars relx* sx* cx*, absorb(year#bbb cs_id#bbb) vce(cluster bbb_s_id bbb_c_id), if !missing(bbb)
est store csdiff


estout firms1 firms2 firmsdiff cs1 cs2 csdiff  , order( ln_prop hipaydayprop hipayday relx_ln_prop ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 
*	 
		 
# delimit ;
esttab firms1 firms2 firmsdiff cs1 cs2 csdiff  ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(sfe cfe yfe csfe  cyfe syfe r2_a N ,  fmt( %20s %20s %20s %20s %20s %20s %9.3fc %9.0fc) label("Supplier FE" "Customer FE" "Year FE" "Pair FE"  "Customer x Year FE" "Supplier x Year FE" "R2" "Observations"))  label collabels(none) title("Cross-Sectional Variation in Customer Pay Speed")     mtitles("High DP" "No Monitor" "Difference" "Monitor" "No Monitor" "Difference" "Monitor" "No Monitor" "Difference" "Interaction") 
	order( ln_prop hipaydayprop hipayday relx_ln_prop ) keep(ln_prop hipaydayprop hipayday relx_ln_prop ) 
;
#delimit cr


/*****
FIGURE 1:UNIVARIATE PATTERNS*/

capture drop deccys
xtile deccys = prop_sale,n(10)

capture drop meantc
bys deccys: egen meantc = mean(sp_arS)

capture drop ars_bench
bys deccys: egen ars_bench = mean(s_ar_revt)

	/*PANEL A*/
label var meantc "Trade Credit"
label var ars_bench "Firm-Level AR/Sales"
twoway (bar meantc deccys,lcolor(black) color(black) fcolor(black) fintensity(inten40)) (connected ars_bench deccys,lcolor(black) mcolor(black)) ,graphregion(color(white)) xtitle("Deciles of Sales Dependence")
graph export salesdep.pdf,replace

capture drop meanARperc
bys deccys: egen meanARperc =mean(ar_perc)

capture drop gap
gen gap = prop_sale-ar_perc
capture drop shortfall
bys deccys: egen shortfall = mean(gap)

capture drop propbench 
bys deccys: egen propbench = mean(prop_sale)

label var meanARperc "Percentage of Receivables"
label var propbench "Percentage of Sales"
label var shortfall "TC Shortfall"
/*PANEL B*/
graph twoway (bar meanARperc   deccys, lcolor(black) color(black) fcolor(black) fintensity(inten40)) (bar  shortfall deccys, lcolor(black) color(black) fcolor(black) fintensity(inten80)) (connected propbench deccys,lcolor(black) mcolor(black)), graphregion(color(white)) xtitle("Deciles of Sales Dependence")


graph export tcsales_gap.pdf,replace


/*
FIGURE 2: MONOTONICITY AND CONSISTENCY OVER TIME
*/
/*show monotonic effect; stick it in a coefplot graph*/
replace use =1
label def decile 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10"
label val deccys decile
 qui reghdfe sp_arS   i.deccys $svars $cvars ,   absorb(year cs_id) nocons vce(cluster $clustvar ), if use ==1
 est store proppy
coefplot proppy , keep(*.deccys)  format(%12.2f) ytitle() recast(bar)  noci asequation swapnames nokey vertical    eqrename(^_(.*)$ = >\1%, regex) nooffsets /*title("Panel A: Coefficients on Sales Dependence Deciles") */ mcolor(black) mlabel mlabcolor(black)    eqlabels(  "2" "3" "4" "5" "6" "7" "8" "9" "10")graphregion(color(white)) bcolor(gray) mlabposition(6)

  reghdfe sp_arS  i.year#c.ln_prop $svars $cvars , absorb(  cs_id) vce(cluster $clustvar ), if use ==1
 est store proppy
coefplot proppy , keep(*.year#c.ln_prop c.ln_prop ln_prop)  format(%12.3f) ytitle() recast(connected)  levels(90) asequation swapnames nokey vertical   eqrename(^_(.*)$ = >\1%, regex) nooffsets title("Panel A: Coefficients on Sales Dependence Deciles")  mcolor(black)   mlabcolor(black) label
